﻿using System;
using System.Data;
using System.Threading.Tasks;

using Newtonsoft.Json;

using Stubble.Core.Builders;
using Stubble.Extensions.JsonNet;
using Stubble.Extensions.SystemData;

namespace MustacheDemo
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            //await Test1Async();
            //await Test2Async();
            //await Test3Async();
            //await Test4Async();
            //await Test5Async();
            await Test6Async();
        }

        static async Task Test1Async()
        {
            const string json = "{ foo: \"bar\" }";
            var builder = new StubbleBuilder().Configure(settings => settings.AddJsonNet()).Build();
            var obj = JsonConvert.DeserializeObject(json);
            var output = await builder.RenderAsync("foo {{foo}}", obj);
            Console.WriteLine(output);
        }

        static async Task Test2Async()
        {
            var builder = new StubbleBuilder().Build();
            var output = await builder.RenderAsync("foo {{foo}}", new { foo = "bar" });
            Console.WriteLine(output);
        }

        static async Task Test3Async()
        {
            var builder = new StubbleBuilder().Build();
            var output = await builder.RenderAsync("1 - {{key1}}; 2 - {{key2}}; 3 - {{key3}}", new { key1 = "一", key3 = "三", key = "二" });
            Console.WriteLine(output);
        }

        static async Task Test4Async()
        {
            var dt = new DataTable();
            dt.Columns.Add("IntColumn", typeof(int));
            dt.Rows.Add(1);
            dt.Rows.Add(2);
            dt.Rows.Add(3);

            var builder = new StubbleBuilder().Configure(settings => settings.AddSystemData()).Build();
            var output = await builder.RenderAsync("{{#foo}}{{IntColumn}}{{/foo}}", new { foo = dt });
            Console.WriteLine(output);
        }

        static async Task Test5Async()
        {
            var builder = new StubbleBuilder().Build();
            var output = await builder.RenderAsync("{{#show}}显示{{/show}}{{^show}}隐藏{{/show}}", new { show = true });
            Console.WriteLine(output);

            output = await builder.RenderAsync("{{#show}}显示{{/show}}{{^show}}隐藏{{/show}}", new { show = false });
            Console.WriteLine(output);
        }

        static async Task Test6Async()
        {
            var builder = new StubbleBuilder().Build();
            var output = await builder.RenderAsync("{{nbsp}}", new { nbsp = "&nbsp;" });
            Console.WriteLine(output);

            output = await builder.RenderAsync("{{{nbsp}}}", new { nbsp = "&nbsp;" });
            Console.WriteLine(output);
        }
    }
}

// http://mustache.github.io/
// http://mustache.github.io/mustache.5.html
// https://www.nuget.org/packages/Stubble.Core/
// https://github.com/StubbleOrg/Stubble
// https://github.com/StubbleOrg/Stubble/blob/master/docs/compilation.md

// https://github.com/StubbleOrg/Stubble.Extensions.JsonNet
// https://github.com/StubbleOrg/Stubble.Extensions.SystemData
// https://csharp.hotexamples.com/examples/Stubble.Core/StubbleBuilder/-/php-stubblebuilder-class-examples.html
// https://csharp.hotexamples.com/examples/Stubble.Core/Context/-/php-context-class-examples.html
